package com.fl.controller;

import com.fl.utils.QueryBuilder;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;

import org.elasticsearch.common.text.Text;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: fulin
 * @Date: 2021/09/05/20:08
 * @Description:
 */
@RestController
public class UserController {

    @Autowired
    RestHighLevelClient restHighLevelClient;

    @GetMapping("/getHight")
    public String gethight() throws IOException {
        QueryBuilder queryBuilder = new QueryBuilder();
        SearchRequest search = queryBuilder.search();
        SearchResponse searchResponse = restHighLevelClient.search(search, RequestOptions.DEFAULT);
        SearchHit[] hits = searchResponse.getHits().getHits();
        String str = "";
        for (SearchHit hit : hits) {
            HighlightField other = hit.getHighlightFields().get("other");
            for (Text fragment : other.fragments()) {
                str += fragment+"\t";
            }
        }
        return str;
    }
}
